package com.ruoyi.attachments.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.attachments.domain.KwPackageInfo;
import com.ruoyi.attachments.service.IKwPackageInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 安装包管理Controller
 * 
 * @author gongln
 * @date 2023-11-13
 */
@RestController
@RequestMapping("/attachments/attachments")
public class KwPackageInfoController extends BaseController
{
    @Autowired
    private IKwPackageInfoService kwPackageInfoService;

    /**
     * 查询安装包管理列表
     */
    @PreAuthorize("@ss.hasPermi('attachments:attachments:list')")
    @GetMapping("/list")
    public TableDataInfo list(KwPackageInfo kwPackageInfo)
    {
        startPage();
        List<KwPackageInfo> list = kwPackageInfoService.selectKwPackageInfoList(kwPackageInfo);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('attachments:attachments:list')")
    @GetMapping("/all")
    public List<KwPackageInfo> all(KwPackageInfo kwPackageInfo)
    {
        List<KwPackageInfo> list = kwPackageInfoService.selectKwPackageInfoList(kwPackageInfo);
        return list;
    }

    /**
     * 导出安装包管理列表
     */
    @PreAuthorize("@ss.hasPermi('attachments:attachments:export')")
    @Log(title = "安装包管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KwPackageInfo kwPackageInfo)
    {
        List<KwPackageInfo> list = kwPackageInfoService.selectKwPackageInfoList(kwPackageInfo);
        ExcelUtil<KwPackageInfo> util = new ExcelUtil<KwPackageInfo>(KwPackageInfo.class);
        util.exportExcel(response, list, "安装包管理数据");
    }

    /**
     * 获取安装包管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('attachments:attachments:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(kwPackageInfoService.selectKwPackageInfoById(id));
    }

    /**
     * 新增安装包管理
     */
    @PreAuthorize("@ss.hasPermi('attachments:attachments:add')")
    @Log(title = "安装包管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KwPackageInfo kwPackageInfo)
    {
        String separator = "\\|";
        String fileInfo = kwPackageInfo.getPackagePath();
        String oriName = fileInfo.split(separator)[0];
        String fileSize = fileInfo.split(separator)[1];
        String filePath = fileInfo.split(separator)[2];
        kwPackageInfo.setPackageName(oriName);
        kwPackageInfo.setPackagePath(filePath);
        kwPackageInfo.setPackageSize(Long.parseLong(fileSize));
        kwPackageInfo.setPackageType(oriName.endsWith("rpm")?"rpm":"bin");
        kwPackageInfo.setCreateUser(getUsername());
        kwPackageInfo.setUpdateUser(getUsername());
        return toAjax(kwPackageInfoService.insertKwPackageInfo(kwPackageInfo));
    }

    /**
     * 修改安装包管理
     */
    @PreAuthorize("@ss.hasPermi('attachments:attachments:edit')")
    @Log(title = "安装包管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KwPackageInfo kwPackageInfo)
    {
        return toAjax(kwPackageInfoService.updateKwPackageInfo(kwPackageInfo));
    }

    /**
     * 删除安装包管理
     */
    @PreAuthorize("@ss.hasPermi('attachments:attachments:remove')")
    @Log(title = "安装包管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(kwPackageInfoService.deleteKwPackageInfoByIds(ids));
    }
}
